import 'package:app_plugin/app_plugin.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final String defaultConfigJson = "{\"dns\":{\"servers\":[{\"tag\":\"local\",\"address\":\"local\",\"detour\":\"direct\"},{\"tag\":\"proxy\",\"address\":\"tls://223.5.5.5\",\"detour\":\"proxy\"}],\"rules\":[{\"geosite\":\"cn\",\"server\":\"proxy\"},{\"outbound\":\"any\",\"server\":\"local\"}],\"final\":\"local\"},\"inbounds\":[{\"type\":\"tun\",\"tag\":\"tun-in\",\"inet4_address\":\"172.19.0.1/30\",\"inet6_address\":\"fdfe:dcba:9876::1/126\",\"auto_route\":true}],\"outbounds\":[{\"type\":\"shadowsocks\",\"tag\":\"proxy\",\"server\":\"shshcua.fosi.hk\",\"server_port\":36354,\"method\":\"aes-256-gcm\",\"password\":\"d515695d-e70d-43f0-bfcc-594419f73bcc\",\"udp_over_tcp\":false},{\"type\":\"direct\",\"tag\":\"direct\"},{\"type\":\"dns\",\"tag\":\"dns-out\"}],\"route\":{\"rules\":[{\"protocol\":\"dns\",\"outbound\":\"dns-out\"},{\"domain_regex\":[\"v2b.8iack.eu.org\",\"v2b.8iack.eu.org\",\"v2b.8iack.eu.org\"],\"outbound\":\"direct\"},{\"geosite\":\"cn\",\"outbound\":\"direct\"},{\"geoip\":\"cn\",\"outbound\":\"direct\"}],\"final\":\"proxy\",\"auto_detect_interface\":true}}";
  late final TextEditingController _textController = TextEditingController(text: defaultConfigJson);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(12.0),
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              TextField(
                  controller: _textController,
                  decoration: const InputDecoration(
                    labelText: '输入config json',
                  ),
                  maxLines: null,
              ),
              Container(
                margin: const EdgeInsets.only(top: 8),
                child: Row(
                  children: [
                    ElevatedButton(
                      onPressed: () {
                        // 按钮点击事件处理
                        String inputValue = _textController.text;
                        AppPlugin.instance().startVpn(inputValue);
                      },
                      style: ElevatedButton.styleFrom(
                        primary: Colors.blue,  // 设置按钮背景颜色为蓝色
                        onPrimary: Colors.white,  // 设置字体颜色为白色
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5.0),  // 设置圆角半径
                        ),
                      ),
                      child: const Text('开启VPN'),
                    ),
                    const SizedBox(width: 16,),
                    ElevatedButton(
                      onPressed: () {
                        AppPlugin.instance().stopVpn();
                      },
                      style: ElevatedButton.styleFrom(
                        primary: Colors.deepOrange,
                        onPrimary: Colors.white,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5.0),  // 设置圆角半径
                        ),
                      ),
                      child: const Text('关闭VPN'),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
